// pages/Main/mainIndex/mainIndex.js
var wxCharts = require('../../../utils/Component/wxChart/wxcharts.js');
var lineChart = null;
var startPos = null;
var common = require('../../../utils/common.js')
var businessLogic = require('../../../utils/NetWork/Model/MainbusinessLogic.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    log: [],
    banji: '',
    shijian: '',
    defenlv: '',
    extype:'',
    filed: ',,,,',
    title:'',
    ur:'',
  },
  touchHandler: function (e) {
    lineChart.scrollStart(e);
    for (var a = 0; a < this.data.log.length; a++) {
      if (lineChart.getCurrentDataIndex(e) == a) {
        this.setData({
          banji: this.data.log[a].examid_exam_title,
          shijian: this.data.log[a].newstime,
          defenlv: (this.data.log[a].score / this.data.log[a].paperid_testpaper_point * 100).toFixed(2)+ "%",
          examid: this.data.log[a].examid,
          testid: this.data.log[a].examid_exam_paperid,
          extype: this.data.log[a].examtype,
          type: this.data.log[a].statemark,
        });
      }
    }
  },
  moveHandler: function (e) {
    lineChart.scroll(e);
  },
  touchEndHandler: function (e) {
    lineChart.scrollEnd(e);
    lineChart.showToolTip(e, {
      format: function (item, category) {
        return category + ' ' + item.name + ':' + item.data
      }
    });
  },
  createSimulationData: function () {
    var categories = [];
    var data = [];
    for (var i = 0; i <this.data.log.length; i++) {
      categories.push(this.data.log[i].newstime);
      data.push(this.data.log[i].score / this.data.log[i].paperid_testpaper_point*100);
    }
    return {
      categories: categories,
      data: data
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    let c = common.Getorgid();
    if (c.role == "学生") {
      this.setData({
        orgid: c.orgid,
        userid: c.userid,
      });
    }
    else {
      this.setData({
        orgid: c.orgid,
        userid: e.id,
      });
    }
    this.learn();
  },
  learn: function () {
    let params = {
      "orgid": this.data.orgid, "userid": this.data.userid, "searchField": "peopleid,statemark,examid|考试系统模型|title,kenids|数据词典|title,subject|数据词典|title,difficulty|数据词典|title,semester|数据词典|title", "searchValue": "" + this.data.userid + ",1," + this.data.title + "," + this.data.filed}
    businessLogic.QueryLog(params, (res) => {
      console.log("8.14.1", res)
      if (res.length > 0) {
      this.setData({
        log: res,
        banji: res[0].examid_exam_title,
        shijian:res[0].newstime,
        defenlv: (res[0].score / res[0].paperid_testpaper_point * 100).toFixed(2) + "%",
        examid: res[0].examid,
        testid: res[0].examid_exam_paperid,
        extype: res[0].examtype,
        type: res[0].statemark,
      });
      var windowWidth = 320;
      try {
        var resa = wx.getSystemInfoSync();
        windowWidth = resa.windowWidth;
      } catch (e) {
        console.error('getSystemInfoSync failed!');
      }

      var simulationData = this.createSimulationData();
      lineChart = new wxCharts({
        canvasId: 'lineCanvas',
        type: 'line',
        categories: simulationData.categories,
        animation: false,
        series: [{
          name: '得分率',
          data: simulationData.data,
          format: function (val, name) {
            return val.toFixed(2) + '%';
          }
        }],
        xAxis: {
          disableGrid: false
        },
        yAxis: {
          title: '考试得分率',
          format: function (val) {
            return val.toFixed(2);
          },
          min: 0
        },
        width: windowWidth,
        height: 200,
        dataLabel: true,
        dataPointShape: true,
        enableScroll: true,
        extra: {
          lineStyle: 'curve'
        }
      });
      }
      else{
        this.setData({
          log: "暂无数据",
          banji: "暂无数据",
          shijian: "暂无数据",
          defenlv: "暂无数据",
          examid:'',
          testid: '',
        });
      }
    })
  },
  //搜索
   bindKeyInput: function (e) {
    this.setData({
      title: e.detail.value
    })
  },
  search2: function (event) {
    let params = { "orgid": this.data.orgid, "userid": this.data.userid, "searchField": "peopleid,statemark,examid|考试系统模型|title,kenids|数据词典|title,subject|数据词典|title,difficulty|数据词典|title,semester|数据词典|title", "searchValue": "" + this.data.userid + ",1," + this.data.title + "," + this.data.filed}
    businessLogic.QueryLog(params, (res2) => {
      //console.log("search",res2)
      if (res2.length > 0) {
       this.setData({
        banji: res2[0].examid_exam_title,
        shijian: res2[0].newstime,
        defenlv: (res2[0].score / res2[0].paperid_testpaper_point * 100).toFixed(2) + "%",
        examid: res2[0].examid,
        testid: res2[0].examid_exam_paperid,
        extype: res2[0].examtype,
        type: res2[0].statemark,
      }); 
      }
      else{
        this.setData({
          banji: "暂无此日志",
          shijian: "暂无此日志",
          defenlv: "暂无此日志",
          examid: '',
          testid: '',
        });
      }
    });
  }, 
  stuTestall: function () {
    if (this.data.type == 1){
      wx.navigateTo({
        url: "../stuTestall/stuTestall?parid=" + this.data.testid + "&&examid=" + this.data.examid +"&&type=已批改"
    });  
    }else{
      wx.navigateTo({
        url: "../stuTestall/stuTestall?parid=" + this.data.testid + "&&examid=" + this.data.examid + "&&type=未批改"
    });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.ConditionList = this.selectComponent("#ConditionList");
    this.ConditionList.close();
    this.shanxuan();
  },
  isShow: function (e) {
    this.ConditionList.isShow();
    this.search2();
  },
  //筛选
  shanxuan: function (ur) {
    if (ur == '' || ur == undefined) {
      var ur = '';
      let params = {
        "classid": "5b3bbf31-44be-483b-93c5-7229c486b85d", "orgid": this.data.orgid, "ur": ur
      }
      businessLogic.getCondition(params, (res) => {
        //渲染页面
        this.ConditionList.conditionList(res, ur);
      })
    } else {
      console.log(ur.detail)
      let params = {
        "classid": "5b3bbf31-44be-483b-93c5-7229c486b85d", "orgid": this.data.orgid, "ur": ur.detail[0]
      }
      businessLogic.getCondition(params, (res) => {
        //渲染页面
        this.setData({
          ur: ur.detail[0],
          filed: ur.detail[1]
        })
        this.ConditionList.conditionList(res, ur.detail[0]);

      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})